﻿// owner 蒋家伟

#include "StorageWidget.h"

// qt
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>

// jsdk
#include "Dbg_Warn_Define.h"
#include "UIBaseUtils.h"

using namespace jsdk;


OwnerPtr<jsdk::StorageWidget> jsdk::StorageWidget::create(const std::wstring& rootFilePath, bool isOnlyDisplayDir)
{
    OwnerPtr<jsdk::StorageWidget> opStorageWidget = NEW_AS_OWNERPTR(jsdk::StorageWidget, rootFilePath, isOnlyDisplayDir);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opStorageWidget, L"存储窗口创建失败", L"蒋家伟", L"2023-10-24");
    opStorageWidget->m_storageRootPath = QString::fromStdWString(rootFilePath);
    if (opStorageWidget->m_storageRootPath.isEmpty())
        return nullptr;
    opStorageWidget->updateStoragePresent();
    return std::move(opStorageWidget);
}

void jsdk::StorageWidget::show()
{
    QWidget::show();
}

void jsdk::StorageWidget::updateStoragePresent()
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_pTotalStorages && m_pUserdStorages && m_wpProgressBar, L"更新内存占比失败， 控件无效", L"蒋家伟", L"2023-10-24");
    m_storageInfo = QStorageInfo(m_storageRootPath);
    if (!m_storageInfo.isValid())
        return;
    qint64 totalByteSize = m_storageInfo.bytesTotal();
    m_pTotalStorages->setText(u8"内存总量: " + UIBaseUtils::getSizeStr(totalByteSize));
    qint64 usedByteSize = totalByteSize - m_storageInfo.bytesAvailable();
    m_pUserdStorages->setText(u8"使用总量: " + UIBaseUtils::getSizeStr(usedByteSize));

    double rate = static_cast<double>(usedByteSize) / totalByteSize * 100;

    if (std::isgreaterequal(rate, 80))
    {
        // 如果超过80%为红色
        m_wpProgressBar->setColor(Qt::GlobalColor::red);
    }
    else if (std::isgreaterequal(rate, 40.0))
    {
        // 如果超过40-80为黄色
        m_wpProgressBar->setColor(Qt::GlobalColor::yellow);
    }
    else
    {
        // 40以下为绿色
        m_wpProgressBar->setColor(Qt::GlobalColor::green);
    }
    m_wpProgressBar->setPresent(static_cast<uint8_t>(rate));
}

jsdk::StorageWidget::StorageWidget(const std::wstring& rootFilePath, bool isOnlyDisplayDir)
{
    QVBoxLayout* pMainLayout = new QVBoxLayout;

    {
        QHBoxLayout* pStorageLayout = new QHBoxLayout;
        m_pTotalStorages = new QLabel;
        m_pTotalStorages->setFixedWidth(150);
        m_pUserdStorages = new QLabel;
        m_pUserdStorages->setFixedWidth(150);
        m_wpProgressBar = ProgressBar::create(true, nullptr).release();
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_wpProgressBar, L"百分比创建失败", L"蒋家伟", L"2023-10-24");
        m_wpProgressBar->setFixedSize(100, 100);
        m_wpProgressBar->setPrefix(L"使用率: ");
        m_wpProgressBar->setFontSize(8);

        pStorageLayout->addWidget(m_pTotalStorages);
        pStorageLayout->addWidget(m_pUserdStorages);
        pStorageLayout->addWidget(m_wpProgressBar.get());

        pMainLayout->addLayout(pStorageLayout);
    }

    {
        m_wpResourceManagerWidget = ResourceManagerWidget::create({ rootFilePath }, false, isOnlyDisplayDir).release();
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_wpResourceManagerWidget, L"资源管理器创建失败", L"蒋家伟", L"2023-10-24");

        {
            // 信号与槽连接
            bool success = connect(m_wpResourceManagerWidget.get(), &ResourceManagerWidget::deleteNodeSuccess, this, &StorageWidget::updateStoragePresent);
            DBG_WARN_AND_RETURN_VOID_UNLESS(success, L"更新内存占比失败", L"蒋家伟", L"2023-10-24");
        }

        pMainLayout->addWidget(m_wpResourceManagerWidget.get());
    }


    this->setMinimumSize(450, 600);
    this->setWindowTitle(u8"存储管理");
    this->setLayout(pMainLayout);
}
